package com.lxh.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lxh.entity.User;
import com.lxh.entity.dto.UserDto;
import com.lxh.entity.vo.UserVo;
import com.lxh.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lxh
 * @since 2022-03-01
 */
@Api("用户前端控制器")
@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    /**
     * 获取列表
     * @param dto /
     * @return java.util.List<com.lxh.entity.vo.UserVo> /
     * @author lxh
     * @date 2022/3/1 18:45
     */
    @ApiOperation(value = "获取列表")
    @GetMapping("/getList")
    public List<UserVo> getList(UserDto dto) {
        return userService.getList(dto);
    }

    /**
     * 分页查询
     * @param dto /
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.lxh.entity.vo.UserVo> /
     * @author lxh
     * @date 2022/3/1 18:46
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/getPage")
    public IPage<UserVo> getPage(UserDto dto) {
        return userService.getPage(dto);
    }

    /**
     * 删除
     * @param id /
     * @return java.lang.String /
     * @author lxh
     * @date 2022/3/1 18:48
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/remove")
    public String remove(Long id) {
        return new User().deleteById(id) ? "删除成功" : "删除失败";
    }

    /**
     * 新增
     * @param user /
     * @return java.lang.String /
     * @author lxh
     * @date 2022/3/1 18:53
     */
    @ApiOperation(value = "新增")
    @PostMapping("/saveData")
    public String saveData(@RequestBody User user) {
        return user.insert() ? "新增成功" : "新增失败";
    }

    /**
     * 修改
     * @param user /
     * @return java.lang.String /
     * @author lxh
     * @date 2022/3/1 18:56
     */
    @ApiOperation(value = "修改")
    @PutMapping("/updateData")
    public String updateData(@RequestBody User user) {
        return user.insertOrUpdate() ? "修改成功" : "修改失败";
    }

}
